﻿using System;
using System.ComponentModel;
using System.Data;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class LocalTeamGroupForm : DevExpress.XtraEditors.XtraForm
    {
        public LocalTeamGroupForm()
        {
            InitializeComponent();
        }

        private void LocalTeamGroupForm_Load(object sender, EventArgs e)
        {
            #region 初始化查询条件

            //默认为本月1日起，下下月1日止
            dateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Year, DateTime.Now.Month ));
            dateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Year + DateTime.Now.Month / 11, (DateTime.Now.Month + 1) % 12 + 1));

            btnRefresh.PerformClick();
            
            #endregion
        }

        //查询团数据
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DevExpress.Utils.ToolTipController tip = ApplicationHelper.ShowLoadTip("正在加载数据...", teamGridControl);

            //记住当前选中行‘计划号’
            string focusedTourgroupID = "";
            if (teamGridView.FocusedRowHandle >= 0)
                focusedTourgroupID = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupID").ToString();//.FocusedRowHandle;
            //int focusedHandle = teamGridView.FocusedRowHandle;

            //TeamStateEnum teamState = comboStatus.EditValue == null ? TeamStateEnum.全部 : EnumHelper.Team_Str2Enum(comboStatus.EditValue.ToString(), true);
            DateTime dateStart = DateTime.Parse(this.dateStart.EditValue.ToString());
            DateTime dateEnd = DateTime.Parse(this.dateEnd.EditValue.ToString());

            this.teamGridControl.DataSource = TeamGroupCtrl.GetTeamGroupData(dateStart, dateEnd);

            //聚焦刷新前的行
            DataTable dt = teamGridControl.DataSource as DataTable;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["TourgroupID"].ToString().Trim() == focusedTourgroupID.Trim())
                {
                    teamGridView.FocusedRowHandle = teamGridView.GetRowHandle(i);
                    break;
                }
            }
            //teamGridView.FocusedRowHandle = focusedHandle;
            tip.HideHint();
        }

        //团数据行单击事件
        private void teamGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                Cursor.Current = Cursors.WaitCursor;

                string tourGroupId = teamGridView.GetRowCellValue(e.RowHandle, "TourgroupID").ToString();
                DataTable dt = teamGridControl.DataSource as DataTable;
                dt.DefaultView.RowFilter = string.Format("TourgroupID='{0}'", tourGroupId);

                if (TeamGroupCtrl.GetTourGroupState(tourGroupId) == TeamStateEnum.计划)
                {
                    //此团没有成行，不能处理。避免未成行团产生预订后，预订中团名与团计划中的团名不一致。
                    MessageBox.Show("该团尚未成行！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (LocalTeamGroupCtrl.GetLocalPlanSchedule(tourGroupId).Tables[0].Rows.Count == 0)
                {
                    //1、没有本地计划，则设置团行数据的‘计调’操作状态为0，无色显示
                    TeamGroupCtrl.UpdateTourGroupLocalTourgroupRowState(tourGroupId, 0);
                    
                    #region 检测大行程中是否存在‘本地’的计划，如有传到本地计划表中
                    string travelAgencyID;
                    bool bStartCopy = false;
                    string ScheduleIDs = "";

                    //2、拷贝本地行程信息到本地
                    DataTable dtSchedule = TeamGroupCtrl.GetTourGroupSchedule(tourGroupId).Tables[0];
                    for (int i = 0; i < dtSchedule.Rows.Count; i++)
                    {
                        travelAgencyID = dtSchedule.Rows[i]["TravelAgencyID"].ToString();
                        if (BaseInfoCtrl.GetProviderForIsLocalProvider(travelAgencyID))//从数据库中判断是否为本地供应商 //(travelAgencyID == "龙润国际")
                        {
                            bStartCopy = true;
                            ScheduleIDs += string.Format("|{0}", dtSchedule.Rows[i]["TourGroupScheduleID"]);
                        }
                        else if (travelAgencyID == "-1" && bStartCopy == true)
                        {
                            ScheduleIDs += string.Format("|{0}", dtSchedule.Rows[i]["TourGroupScheduleID"]);
                        }
                        else if (!BaseInfoCtrl.GetProviderForIsTravelProvider(travelAgencyID) && bStartCopy == true)//判断此行供应商类型是否为"旅行社"（也就是为“旅行社|演出”或类似类型的需拷贝）
                        {
                            ScheduleIDs += string.Format("|{0}", dtSchedule.Rows[i]["TourGroupScheduleID"]);
                        }
                        else
                            bStartCopy = false;
                    }
                    if (ScheduleIDs.StartsWith("|"))//表示有符合条件的id号存在
                    {
                        //1、拷贝符合条件的大行程计划到本地行程计划
                        LocalTeamGroupCtrl.CopyTourGroupSchedule2LocalSchedule(ScheduleIDs);
                        //2、拷贝团备注到本地备注
                        LocalTeamGroupCtrl.CopyTourGroupRemark2LocalRemark(tourGroupId);
                        //3、设置团的‘计调’操作状态为1
                        TeamGroupCtrl.UpdateTourGroupLocalTourgroupRowState(tourGroupId, 1);
                        //4、设置刚拷贝的团备注信息到已经加载的团表‘LocalRemark’字段中
                        dt.DefaultView[0]["LocalRemark"] = TeamGroupCtrl.GetTourGroupScheduleLocalRemark(tourGroupId);
                        //启动计划界面
                        LocalPlanScheduleForm localForm = new LocalPlanScheduleForm(dt.DefaultView);
                        localForm.ShowDialog(this);
                    }
                    else
                        MessageBox.Show("未找到本地日程！！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);//此团没有本地行程安排，无需处理！

                    #endregion
                }
                else
                {
                    LocalPlanScheduleForm localForm = new LocalPlanScheduleForm(dt.DefaultView);
                    localForm.ShowDialog(this);
                }
                btnRefresh.PerformClick();
                Cursor.Current = Cursors.Default;
            }
        }

        private void teamGridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle < 0) return;
            object state = teamGridView.GetRowCellValue(e.RowHandle, "LocalTourgroupRowState");

            if (state==null) return;

            switch (state.ToString())
            {
                case "0":
                    e.Appearance.BackColor = Color.Silver;
                    break;
                case "1"://已经首次打开，将大计划的本地行程信息拷贝过来了
                    e.Appearance.BackColor = Color.Plum;
                    break;
                case "2"://此团为已经操作过（手工在计划中设置的）
                    e.Appearance.BackColor = Color.LightSteelBlue;
                    break;
                default:
                    break;
            }
            if (teamGridView.GetRowCellValue(e.RowHandle, "TourgroupState").ToString() == TeamStateEnum.取消.ToString())
                e.Appearance.BackColor = Color.Silver;
        }

    }
}
